/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package shmallarchitect.jpa.jsf;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import shmallarchitect.jpa.controllers.LocumJpaController;
import shmallarchitect.jpa.entities.Locum;

/**
 *
 * @author jaccus
 */
public class LocumConverter implements Converter {

    public Object getAsObject(FacesContext facesContext, UIComponent component, String string) {
        if (string == null || string.length() == 0) {
            return null;
        }
        Long id = new Long(string);
        LocumJpaController controller = (LocumJpaController) facesContext.getApplication().getELResolver().getValue(facesContext.getELContext(), null, "locumJpa");
        return controller.findLocum(id);
    }

    public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Locum) {
            Locum o = (Locum) object;
            return o.getId() == null ? "" : o.getId().toString();
        } else {
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: shmallarchitect.jpa.entities.Locum");
        }
    }

}
